#ifndef STX_PLATFORM_POSIX_THREAD_BARRIER_ATTRIBUTES_HPP
#define STX_PLATFORM_POSIX_THREAD_BARRIER_ATTRIBUTES_HPP

#include <stx/platform/posix/thread/common.hpp>

namespace stx {

namespace posix {

class barrier_attributes: noncopyable
{
public:
    
    typedef barrier_attributes this_type;
    
    barrier_attributes()
    {
        int ret = pthread_barrierattr_init(&handle_);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_barrierattr_init");
        }
    }
    
    ~barrier_attributes()
    {
        int ret = pthread_barrierattr_destroy(&handle_);
        if (ret) {
            STX_DESTRUCTOR_SYSTEM_ERROR_VAL(ret, "pthread_barrierattr_destroy");
        }
    }
    
    //  Values:
    //      PTHREAD_PROCESS_SHARED
    //      PTHREAD_PROCESS_PRIVATE
    int process_shared() const
    {
        int val;
        int ret = pthread_barrierattr_getpshared(&handle_, &val);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_barrierattr_getpshared");
        }
        return val;
    }
    
    this_type& set_process_shared(int val)
    {
        int ret = pthread_barrierattr_setpshared(&handle_, val);
        if (ret) {
            STX_THROW_SYSTEM_ERROR_VAL(ret, "pthread_barrierattr_setpshared");
        }
        return *this;
    }
    
    typedef pthread_barrierattr_t handle_type;
    
    handle_type* handle()
    {
        return &handle_;
    }
    
    const handle_type* handle() const
    {
        return &handle_;
    }
    
private:
    
    handle_type handle_;
};

} // namespace posix

} // namespace stx

#endif // STX_PLATFORM_POSIX_THREAD_BARRIER_ATTRIBUTES_HPP
